<?php
class Users extends DBTable{
    protected $_table = 'users';
 
    public function __construct(mainframe & $mainframe){
        $this->m =  $mainframe;
        $this->DBTable('users', 'id', $this->m->_db);
    }
    
    public function switchGameMode(){
        //$_SESSION[$this->m->_user->id . ".playforfun"] = $_SESSION[$this->m->_user->id . ".playforfun"] == 0 ? 1 : 0;
        
        $_SESSION[$this->m->_user->id . ".playforfun"] = $_SESSION[$this->m->_user->id . ".playforfun"] == 0 ? 1 : 0;
        $_SESSION[$this->m->_user->id . ".mode"] = (int)$_GET['mode'];
    }
        
    public function changePassword($redirect='/account/password/?act=success'){
        if($_SERVER['REQUEST_METHOD'] !='POST') return;
        // Меняем пароль
        $this->passwordValidation();
        
        if (!$this->validation) {
            $this->error->message = _("Incorrectly filled fields!") ;
        } else {
            //$row = new User($this->m);
            $newPassword = $_POST["newpassword"];

            $salt = makePassword(16);
            $crypt = md5(md5($newPassword).$salt);

            $row->password = $crypt . ':' . $salt;
            $row->id = $this->m->_user->id;

            if ($this->m->_db->updateObject('users',$row,'id')) {
                $this->m->add_to_history($this->m->_user->id, "account", "changedpassword");
                unset($_POST);
                $_POST['_password'] = array( 'ok' => true, 'message' => _("Password changed!"));
                redirect($redirect);
            } else {
                $_POST['_password'] = array( 'ok' => false, 'message' => _("An unexpected error occurred. Please try again later.") );
            }
        }
    }
    
    private function passwordValidation($length=50){
        $this->validation = true;
        
        $password = $_POST['password'];
        $this->checkPasswordValidation($password);
        $newpassword = $_POST["newpassword"];
        $confirm = $_POST["confirm"];

        //check Password
        if(empty($newpassword)){
            $this->validation = false;
            $this->error->newpassword = _("You need to enter your new password!");
        }else if(strlen($newpassword) > 50){
            $this->validation = false;
            $this->error->newpassword = sprintf(_("Your new password has to be less then %s symbols!"),$length);;
        }

        //check Confirmation
        if(empty($confirm)){
            $this->validation = false;
            $this->error->confirm = _("Confirm entered password!");
        }else if($newpassword != $newpassword){
            $this->validation = false;
            $this->error->confirm = _("Passwords do not match!");
        }
    }
    
     
     //Валидация Даты Рождения
    private function checkBirthday($_day,$_month,$_year){
        $birthday = (empty($_year) ? '0000' : $_year) . "-" . (empty($_month) ? '00' : $_month) . "-" . (empty($_day) ? '00' : $_day);
        
        //p($birthday);
        if(empty($_day) || empty($_month) || empty($_year)){
            $this->validation = false;
            $this->error->birthday = _("Incorect Birthday");

            return;
        }else if (empty($birthday) || strlen($birthday) > 10 || !checkdate($_month, $_day, $_year) || (date("Y") - $_year) <= 16) {
            $this->validation = false;
            $this->error->birthday = _("You can register only if you are 18 years old!");

            return;
        }
        $birthday = date('Y-m-d H:i:s',strtotime($birthday));
        
        return $birthday;
    }
    
    public function changePersonalInfo(){
        if($_SERVER['REQUEST_METHOD'] !='POST') return;
        
        $this->validation = true;
        
        $row = new stdClass;
        $row->country = $this->checkCountry($_POST['country']);
        $email = $this->checkPersonalInfoEmail($_POST['email']);
        if($email) $row->email = $email;
        
        $row->phone = $this->checkPhone($_POST['phone']);
                
        $this->checkPasswordValidation($_POST['password']);
        
        $row->last_modified = date('Y-m-d H:i:s');
        $row->id = $this->m->_user->id;
        
        if ( $this->validation === false ) {
            $this->error->main = _("incorrectly filled");
            return $this->error;
        }

        makeHtmlSafe($row);
        if($this->m->_db->updateObject('users',$row,'id')){
            
            $to_hist["old"]["email"] = $row->email;
            $to_hist["new"]["email"] = $this->m->_user->email;
            
            $to_hist["old"]["firstname"] = $row->firstname;
            $to_hist["new"]["firstname"] = $this->m->_user->firstname;
            
            $to_hist["old"]["lastname"] = $row->country;
            $to_hist["new"]["lastname"] = $this->m->_user->country;
            
            $to_hist["old"]["country"] = $row->country;
            $to_hist["new"]["country"] = $this->m->_user->country;
            
            $to_hist["old"]["phone_prefix"] = $row->phone_prefix;
            $to_hist["new"]["phone_prefix"] = $this->m->_user->phone_prefix;
            
            $to_hist["old"]["phone_area"] = $row->phone_area;
            $to_hist["new"]["phone_area"] = $this->m->_user->phone_area;
            
            $to_hist["old"]["phone_phone"] = $row->phone;
            $to_hist["new"]["phone_phone"] = $this->m->_user->phone;

            $this->m->add_to_history($this->m->_user->id, "account", "changepersonalinfo",serialize($to_hist));
            
            $this->m->_user->email = $row->email;
            $this->m->_user->firstname = $row->firstname;
            $this->m->_user->lastname = $row->lastname;
            $this->m->_user->country = $row->country;
            $this->m->_user->phone_prefix = $row->phone_prefix;
            $this->m->_user->phone_area = $row->phone_area;
            $this->m->_user->phone = $row->phone;
        }
    }
    
    public function checkPasswordValidation($password){
        $this->m->_db->setQuery(
                    "SELECT `users`.`password` FROM `users` WHERE `users`.`id` = ". $this->m->_user->id." LIMIT 1" 
                );

        list($hash, $salt) = explode(':', $this->m->_db->loadResult());
        
        $cryptpass = md5(md5($password).$salt);
        echo $cryptpass;

        /*if ($hash != $cryptpass) {
            $this->validation = false;
            $this->error->password = _("Wrong Password");
            return $this->error;
        }*/
 
 
    }
    
    public function registration($reg){
       
       // if($_SERVER['REQUEST_METHOD'] !='POST') return;
        
        /*
        $this->validation = true;

        $row = new stdClass;
        //$row->demo = (int)$_POST['demo'];
        
        $row->firstname = $this->checkFirstname($reg['firstname']);
        $row->lastname = $this->checkLastname($reg['lastname']);
        $row->birthday = $this->checkBirthday($reg['day'],$_POST['month'],$_POST['year']);
        
        $row->country = $this->checkCountry($_POST['country']);
        //$row->phone = $this->checkPhone($_POST['phone']);
        $row->phone = $this->checkPhoneRegistration($_POST['phone'],$row->country);
        
        $row->email = $this->checkEmail($reg['email']);
        
        $row->password = $this->checkPassword($_POST['password']);
        $row->partner = $this->checkCampaignCode($_POST["campaign_code"]);
        $this->checkConfPassword($_POST['password'],$_POST['conf_password']);
        
        $row->funbalance = 50000;
        //if($row->demo) $row->balance = 50000;
        $this->checkTAC($_POST['tac']);

        $row->date = date('Y-m-d H:i:s');
        
        if ( $this->validation === false ) {
            $this->error->main = _("Wrong filled fields");
            return $this->error;
        }

        makeHtmlSafe($row);
        
        if(!$this->m->_db->insertObject($this->_table,$row)) return;
                    
        include(XPATH_TEMPLATE_FRONT.DS."modules".DS.'email'.DS."emailreg.php");
        $status = (int)sendemail($row->email, $mailsubject, $mailbody_html, $mailbody_txt);
                
        $_POST["email"] = $row->email;
        $_POST["password"] = $this->pswrd;
        $this->m->_auth->login('/account/deposit/cc/');
         
         */
        
        $row = new stdClass();
        $row->firstname = $this->checkFirstname($reg['first_name']);
        $row->lastname = $this->checkLastname($reg['last_name']);
        $row->password=$this->checkConfPassword($reg['password'],$reg['password_confirmation']);
        $row->email=$this->checkEmail($reg['email']);
        
        if ( $this->validation === false ) {
            $this->error->main = _("Wrong filled fields");
            return $this->error;
        }else{
            $this->m->_db->insertObject('users',$row);
        }

        //if(checkAll())
       // if($this->m->_db->insertObject('users',$row)){die(123);};
            
        
        /*
        $this->m->_db->updateObject($row,'id');
        $this->m->_db->setQuery("UPDATE users SET `users`.`username` = 'dasd' WHERE users.id = 1");
        $this->m->_db->query();
               *//* $this->m->_db->setQuery(
                        "INSERT INTO table_name (email, column2, column3) VALUES ($reg['email']], $reg[''], value3,...)"
         )){}*/
    }
    
    private function checkCampaignCode($campaing_code){
      $campaign_code = trim($campaing_code);

        if ($campaign_code != "") {
            
            if (!preg_match('/^[a-zA-Z0-9@_*-]{3,10}$/', $campaign_code)) {
                $this->error["campaign_code"] = _("This username is not found in our system");
                $this->validation = false;
            } else {
                
                $this->m->_db->setQuery(
                        " SELECT `id` "
                        . " FROM `partners` "
                        . " WHERE `username` = " . $this->m->_db->Quote($campaign_code)
                        . " LIMIT 1;"
                    );
                $partner_id = intval($this->m->_db->loadResult());
                
                if (0 == $partner_id) {
                    $this->error["campaign_code"] = _("This username is not found in our system");
                    $this->validation = false;
                } else {
                    return $partner_id;
                }
            }
        }
        //"campaign_code_incorrect" => "Данное имя пользователя отсутствует в нашей системе!"
    }

    private function checkTAC($tac){
        if(!isset($tac)){
            $this->validation = false;
            $this->error->tac = _("Read the terms and conditions");
            return $this->error;
        }
    }
    
    /*private function checkCurrency($currency){
        
    }*/
    
    private function checkCountry($country){
        $country = (int)$country;
        if(!$country){
            $this->validation = false;
            $this->error->country = _("You must choose a country");
            return $this->error;
        }
        
        return $country;
    }
    
    private function checkConfPassword($password,$password2){
        $password = trim($password);
        if($password != $password2 || strlen($password2) == 0){
            $this->error->password2 = _("Passwords do not match");
            $this->validation = false;
            return false;
        } else {
            return($this->checkPassword($password));
        }
    }
    private function checkPassword($password){
        $password = trim($password);
        
        if (empty($password) || strlen($password) < 4 || strlen($password) > 40) {
            $this->error->password = _("You have used incorrect symbols, or incorrect length");
            $this->validation = false;
            return false;
        }else if(!preg_match('/^([a-z0-9])+$/i',$password)){
            
            $this->error->password = _("You have used incorrect symbols, or incorrect length");
            $this->validation = false;
            return false;
        }
        
        //$this->pswrd = $password;
        $salt   = makePassword(16);
        $crypt  = md5(md5($password) . $salt);
        $password  = $crypt . ':' . $salt;
        
        return $password;
    }
    
    public function checkPersonalInfoEmail($email){
        $demo = $this->m->_user->demo;
        $email = strtolower(trim($email));
        
        if (empty($email) || !is_email($email) || strlen($email) > 40 ) {
            $this->error->email = _("You did not enter the e-mail, or incorrect length");
            $this->json = '{"status":"error","message":_("You did not enter the e-mail, or incorrect length")}';
            $this->validation = false;
            return false;
        }else {
            $this->m->_db->setQuery( "SELECT `id`"
                        . " FROM `users` "
                        . " WHERE `users`.`email` = '".$email."'"  
                        //. " AND `users`.`demo` = ".(int)$demo
                        . " LIMIT 1;"
                    );
            $result = $this->m->_db->loadObjectList();  
            
            
            if(!empty($result)){
                if($result[0]->id == $this->m->_user->id){
                    return false;
                }else if($demo){
                    $this->error->email = _("This user is already registred in demo mode");   
                }else{
                    $this->error->email = _("This user is already registred");
                }
                
                $this->json = '{"status":"error","message":'._("This user is already registred").'}';
                $this->validation = false;
                return false;
            }
        }

        return $email;
    }
    
    public function checkEmail($email){
        $email = strtolower(trim($email));
        
        
        if (empty($email) || !preg_match( '/^(([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9\-]+)\.[a-zA-Z0-9\-.]+$)/', $email )  || strlen($email) > 40 ) {
            
            $this->error->email = _("You did not enter e-mail, or incorrect length");
            $this->json = '{"status":"error","message":"'._("You did not enter e-mail, or incorrect length").'"}';
            $this->validation = false;
            return false;
        }else {
            
            $this->m->_db->setQuery( "SELECT `id`"
                        . " FROM `users` "
                        . " WHERE `users`.`email` = '".$email."'"  
                        //. " AND `users`.`demo` = ".(int)$demo
                        . " LIMIT 1;"
                    );
            $result = $this->m->_db->loadObjectList();  
            
            if(!empty($result)){
                
                if($demo){
                    $this->error->email = _("This e-mail is already registred in demo account");   
                }else{
                    $this->error->email = _("This e-mail is already registred");
                }
                
                $this->json = '{"status":"error","message":"'._("This e-mail is already registred").'"}';
                $this->validation = false;
                return false;
            }
        }
        $this->json = '{"status":"success"}';

        return $email;
    }
    private function checkLastname($lastname){
        $lastname = strip_tags(trim($lastname));
        
        if(!$lastname || strlen($lastname) > 40 || strlen($lastname) < 4){
            $this->error->lastname = _("You did not enter the name, or incorrect length");
            $this->validation = false;
            return false;
        }
        return $lastname;
    }
    private function checkFirstname($name){
        $name = strip_tags(trim($name));
        if(!$name || strlen($name) > 40 || strlen($name) < 4){
            
            $this->error->firstname = _("You did not enter the name, or incorrect length");
            $this->validation = false;
            return false;
        }
        return $name;
    }
    
    private function checkLogin($name){
        $name = strip_tags(trim($name));
        
        if(!$name || strlen($name) > 40 || strlen($name) < 4){
            $this->error->firstname = _("You did not enter the login, or incorrect length");
            $this->validation = false;
            return false;
        }
        return $name;
    }
    
    
    
    private function checkPhoneRegistration($phone,$country){
        $phone = preg_replace("/(\D+)/",'',$phone);
        
        if(!$phone){
            $this->error->phone = _("You did not enter a phone number");
            $this->validation = false;
            return false;
        }else if(strlen($phone) < 9 || strlen($phone) > 14 ){
            $this->error->phone = _("You have entered incorrect phone number length");
            $this->validation = false;
            
            return false;
        }else if(!preg_match('/^[0-9]{9,14}$/',$phone)){
            $this->error->phone = _("You have entered incorrect phone number");
            $this->validation = false;
            
            return false;
        }
        
        //получаем код страны
        $this->m->_db->setQuery(
                    "SELECT `country`.`prefix` FROM `country` WHERE `country`.`id`= ".(int)$country
                    . " LIMIT 1" 
                );
        $country_code = $this->m->_db->loadResult();
        
        return $country_code.$phone;
    }
    /*private function checkPhoneRegistration($area,$phone,$country){
        $phone = preg_replace("/(\D+)/",'',$phone);
        $area = preg_replace("/(\D+)/",'',$area);
        
        if(!$phone || !$area){
            $this->error->phone = _("You did not enter a phone number");
            $this->validation = false;
            return false;
        }else if(strlen($phone) != 7 || (strlen($area) < 2 || strlen($area) > 3)){
            $this->error->phone = _("You have entered incorrect phone number length");
            $this->validation = false;
            
            return false;
        }else if(!preg_match('/^[0-9]{7}$/',$phone) || !preg_match('/^[0-9]{2,3}$/',$area)){
            $this->error->phone = _("You have entered incorrect phone number");
            $this->validation = false;
            
            return false;
        }
        
        //получаем код страны
        $this->m->_db->setQuery(
                    "SELECT `country`.`prefix` FROM `country` WHERE `country`.`id`= ".(int)$country
                    . " LIMIT 1" 
                );
        $country_code = $this->m->_db->loadResult();
        
        return $country_code.$area.$phone;
    }*/
    
    private function checkPhone($phone){
        
        if(!$phone){
            $this->error->phone = _("You did not enter a phone number");
            $this->validation = false;
            
            return false;
        }else if(strlen($phone) >= 20 || strlen($phone) <= 4){
            $this->error->phone = _("You have entered incorrect phone number length");
            $this->validation = false;
            
            return false;
        //}else if(!preg_match('/^([0-9 \-\(\)])+$/',$phone)){
        }else if(!preg_match('/^([\+]{1})?[0-9]{1}([0-9 \-\(\)]){4,20}$/',$phone)){
            
            $this->error->phone = _("You have entered incorrect phone number");
            $this->validation = false;
            
            return false;
        }
        
        $phone = preg_replace("/(\D+)/",'',$phone);
        return $phone;
    }
    
    /*private function checkPhone($prefix,$area,$phone){
        $prefix = preg_replace("/(\D+)/",'',$prefix);
        $area = preg_replace("/(\D+)/",'',$area);
        $phone = preg_replace("/(\D+)/",'',$phone);
        
        
        return array('prefix'=>$prefix,'area'=>$area,'phone'=>$phone);
    }*/
}
?>
